package at.guigu.controller;

import at.guigu.pojo.Brand;
import at.guigu.pojo.User;
import at.guigu.pojo.VO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * SpringMVC获取请求参数
 */
//将Usercontroller放到Spring容器中
@Controller
@RequestMapping(value = "/user")
public class UserController {

    // 获取普通数据类型
    @RequestMapping(value = "/quick1")
    @ResponseBody
    public void save1(@RequestParam("username")String username, @RequestParam("age")int age) throws IOException {
        System.out.println(username);
        System.out.println(age);
    }

    // 获取普通POJO类型
    @ResponseBody
    @RequestMapping("/quick2")
    public void save2(User user) throws IOException {
        System.out.println(user);
    }

    // 获取嵌套POJO类型
    @ResponseBody
    @RequestMapping("/quick22")
    public void save22(Brand brand) throws IOException {
        System.out.println(brand);
    }

    // 获取JSON对象的POJO类型
    @ResponseBody
    @RequestMapping("/quick222")
    public void save222(@RequestBody User user) throws IOException {
        System.out.println(user);
    }


    // 获取数组类型
    @ResponseBody
    @RequestMapping("/quick3")
    public void save3(@RequestParam("strs")String[] strs) throws IOException {
        // 由于直接打印数组时只会打印出其地址，所以将其转为List集合输出到控制台
        System.out.println(Arrays.asList(strs));
    }
    // 获取普通集合类型
    @ResponseBody
    @RequestMapping("/quickpre4")
    public void savepre4(@RequestParam("strs") List<String> strs) throws IOException {
        System.out.println(strs);
    }

    // 获取数组集合类型
    @ResponseBody
    @RequestMapping("/quickpre44")
    public void savepre44(@RequestBody List<String> strs) throws IOException {
        System.out.println(strs);
    }


    // 获取集合类型方式一
    @ResponseBody
    @RequestMapping("/quick4")
    public void save4(VO vo) throws IOException {
        System.out.println(vo);
    }

    // 获取集合类型方式二
    @ResponseBody
    @RequestMapping("/quick5")
    public void save5(@RequestBody List<User> userList) throws IOException {
        System.out.println(userList);
    }

    // 测试@RequestParam注解
    @RequestMapping(value = "/quick6")
    @ResponseBody
    public void save6(@RequestParam(value = "name", required = false, defaultValue = "zhangzhang")String username) throws IOException {
        System.out.println(username);
    }


}